package com.company.cloud.work.modules.mgr.controller;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.work.modules.base.entity.WorkReceiptInfo;
import com.company.cloud.work.modules.base.service.WorkReceiptInfoService;
import com.company.cloud.work.modules.cache.models.WorkReceiptModel;
import com.company.cloud.work.modules.cache.utils.CrmWorkChangeUtils;
import com.company.cloud.work.modules.mgr.controller.req.WorkReceiptQuery4PageParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



@Authc
@RestController
@RequestMapping("/mgr/crm/workReceipt")
@Api(tags="CRM_Mgr_回执管理")
public class WorkReceiptMgrController {
	
	@Autowired
	private WorkReceiptInfoService workReceiptInfoService;
	@Autowired
	private UserRemoteService userRemoteService;
	
	
	@PostMapping("/query")
	@ApiOperation(value="查询", response=WorkReceiptModel.class)
	public BaseResult query(@RequestBody WorkReceiptQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		
		IPage<WorkReceiptInfo> page =  workReceiptInfoService.page(
				new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<WorkReceiptInfo>()
				 .eq("rows_state", EnabledEnum.ENABLED.getCode())
				 .eq("company_no", userInfo.getCompanyNo())
				 .eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
				 .eq(StringUtils.hasText(params.getRelBizType()), "rel_biz_type", params.getRelBizType())
				 .eq(StringUtils.hasText(params.getRelBizId()), "rel_biz_id", params.getRelBizId())
				);
		
		if (page == null || CollectionUtils.isEmpty(page.getRecords())) {
			return BaseResult.ok();
		}
		Set<Integer> userIds=new HashSet<Integer>();
		page.getRecords().forEach(item->{
			if(CheckUtils.hasInt(item.getUserId())) {
				userIds.add(item.getUserId());
			}
		});
		BaseClientResult<Map<Integer, UserSimple>> userMap=userRemoteService.getUserMapByIds(userIds);
		IPage<WorkReceiptModel> data=page.convert(new Function<WorkReceiptInfo, WorkReceiptModel>() {
			@Override
			public WorkReceiptModel apply(WorkReceiptInfo entity) {
				if(CheckUtils.hasClientResult(userMap)){
					return CrmWorkChangeUtils.getWorkReceiptModel(entity, userMap.getData());
				}else{
					return CrmWorkChangeUtils.getWorkReceiptModel(entity,null);
				}

			}
		});
		
		
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/get")
	@ApiOperation(value="获取", response=WorkReceiptModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		WorkReceiptInfo entity=workReceiptInfoService.getById(id);
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		Set<Integer> userIds=new HashSet<Integer>();
		userIds.add(entity.getUserId());
		BaseClientResult<Map<Integer, UserSimple>> userMap=userRemoteService.getUserMapByIds(userIds);
		WorkReceiptModel data;
		if(CheckUtils.hasClientResult(userMap)){
			data= CrmWorkChangeUtils.getWorkReceiptModel(entity, userMap.getData());
		}else{
			data= CrmWorkChangeUtils.getWorkReceiptModel(entity, null);
		}

		return BaseResult.ok(data);
	}
	
}
